import {
	defineStore
} from 'pinia';

export const useWallpaperStore = defineStore('Wallpaper', {
	state: () => ({
		iswallpapercarousel: false,
		wallpaperSettings: {
			desktop: {
				folderPath: '',
				carouselInterval: 5,
				isEnabled: false,
				imageList: []
			},
			lockscreen: {
				folderPath: '',
				carouselInterval: 5,
				isEnabled: false,
				imageList: []
			},
			both: {
				folderPath: '',
				carouselInterval: 5,
				isEnabled: false,
				imageList: []
			}
		},
		currentType: 'desktop' // 当前设置的壁纸类型：desktop、lockscreen或both
	}),

	getters: {
		// 获取当前类型的壁纸设置
		currentSettings: (state) => {
			return state.wallpaperSettings[state.currentType] || state.wallpaperSettings.desktop;
		},
		
		// 获取桌面壁纸设置
		desktopSettings: (state) => {
			return state.wallpaperSettings.desktop;
		},
		
		// 获取锁屏壁纸设置
		lockscreenSettings: (state) => {
			return state.wallpaperSettings.lockscreen;
		},
		
		// 获取同时设置的壁纸设置
		bothSettings: (state) => {
			return state.wallpaperSettings.both;
		}
	},
	
	actions: {
		// 设置当前壁纸类型
		setCurrentType(type) {
			if (type === 'desktop' || type === 'lockscreen' || type === 'both') {
				this.currentType = type;
			}
		},
		
		// 更新壁纸设置
		updateSettings(type, settings) {
			if (type === 'desktop' || type === 'lockscreen' || type === 'both') {
				this.wallpaperSettings[type] = {
					...this.wallpaperSettings[type],
					...settings
				};
			}
		},
		
		// 从本地存储加载设置
		loadSettings() {
			try {
				const desktopSettings = uni.getStorageSync('wallpaperCarouselSettings_desktop');
				const lockscreenSettings = uni.getStorageSync('wallpaperCarouselSettings_lockscreen');
				const bothSettings = uni.getStorageSync('wallpaperCarouselSettings_both');
				
				if (desktopSettings) {
					this.wallpaperSettings.desktop = JSON.parse(desktopSettings);
				}
				
				if (lockscreenSettings) {
					this.wallpaperSettings.lockscreen = JSON.parse(lockscreenSettings);
				}
				
				if (bothSettings) {
					this.wallpaperSettings.both = JSON.parse(bothSettings);
				}
			} catch (error) {
				console.error('加载壁纸设置出错:', error);
			}
		},
		
		// 保存设置到本地存储
		saveSettings() {
			try {
				uni.setStorageSync(
					'wallpaperCarouselSettings_desktop', 
					JSON.stringify(this.wallpaperSettings.desktop)
				);
				
				uni.setStorageSync(
					'wallpaperCarouselSettings_lockscreen', 
					JSON.stringify(this.wallpaperSettings.lockscreen)
				);
				
				uni.setStorageSync(
					'wallpaperCarouselSettings_both', 
					JSON.stringify(this.wallpaperSettings.both)
				);
			} catch (error) {
				console.error('保存壁纸设置出错:', error);
			}
		}
	}
});